<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文件搜索服务系统</title>
    <!--css样式控制-->
    <link rel="stylesheet" href="static/styles/admin.css"/>
    <!-- bower:css -->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
    <!-- jquery:js -->
    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <!--弹窗提示插件-->
    <link href="libs/sweetalert/sweetalert.css" rel="stylesheet" />
    <script src="libs/sweetalert/sweetalert.min.js"></script>
    <!--应用程序脚本-->
    <script src="JAVASCRIPT.js"></script>
    
</head>

<body>
    <div class="admin-container">
        <div class="row">
            <div class="admin-title">
                <h1>文件搜索服务系统</h1>
            </div>
            <div class="admin-content">
                <div class="panel-heading">
                    <h3 class="panel-title">登&nbsp;&nbsp;录</h3>
                </div>
                <div class="login-content">
                    <form role="form">
                        <fieldset>
                            <div class="form-group">
                                <input class="admin-input" placeholder="用户名" name="usernameOrEmailAddress" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="admin-input" placeholder="密码" name="password" type="password">
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input name="remember" type="checkbox">&nbsp;&nbsp;<span>记住我</span> 
                                </label>
                            </div>
                            <a href="javascript:void(0);" class="btn btn-success btn-block" onclick="login();">登&nbsp;&nbsp;录</a>
                        </fieldset>
                    </form>
                </div>
            </div>
            <div class="copyright">
                <p class="text-center">2018 © 广西金普威信息系统有限公司</p>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript">
    // 保存cookie
    function saveCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = 'expires=' + d.toGMTString();
        document.cookie = cname + '=' + cvalue + '; ' + expires;
    };
    // 获取cookie
    function getCookie(cname) {
        var name = cname + '=';
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i].trim();
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return null;
    };
    //错误提示
    function errorTip(title, text) {
        text = text ? text : '';
        sweetAlert({
            title: title, //头部
            text: text + ' 3秒后自动关闭', //主体
            type: "error", //类型
            confirmButtonText: "关闭", //确认按钮文本
            timer: 3000,
        });
    };

    //===============记住账号密码 Start===================
    var inp = document.getElementsByTagName('input');    //页面加载完成 先判断 localStorage中有无name 和pass 
       
    if (window.localStorage.getItem('name') && window.localStorage.getItem('pass')) {    //存在將本地localStorage 值 显示在输入框中
              
        inp[0].value = window.localStorage.getItem('name');      
        inp[1].value = window.localStorage.getItem('pass');      
        inp[2].checked = true; //将选择保存账户密码的复选框更改为选中
           
    };    //当用户登录时,点击保存账户和密码时,将账户名和密码记录到localStorage中
    inp[2].addEventListener('click', function() {      
        if (this.checked) {         
            var n = inp[0].value;         
            var p = inp[1].value;         
            window.localStorage.setItem('name', n);         
            window.localStorage.setItem('pass', p);      
        } else {       //若不点,则把已经记录到localStorge 中的值删除
                     
            window.localStorage.removeItem('name');         
            window.localStorage.removeItem('pass');      
        }   
    });
    //===============记住账号密码 End===================

    //登录
    function login() {
        // var formData = $('form').serializeArray();
        var postData = {};
        // for (var i = 0; i < formData.length; i++) {
        //     //组装提交数据
        //     switch (formData[i].name) {
        //         case 'usernameOrEmailAddress':
        //             postData.usernameOrEmailAddress = formData[i].value;
        //             if (formData[i].value == null || formData[i].value == '') {
        //                 errorTip('【用户名】不能为空！');
        //                 return false;
        //             }
        //             break;
        //         case 'password':
        //             postData.password = formData[i].value;
        //             if (formData[i].value == null || formData[i].value == '') {
        //                 errorTip('【密码】不能为空！');
        //                 return false;
        //             }
        //             break;
        //         default:
        //             break;
        //     }
        // }
        if (inp[0].value == null || inp[0].value == '') {
            errorTip('【用户名】不能为空！');
            return false;
        }
        if (inp[1].value == null || inp[1].value == '') {
            errorTip('【密码】不能为空！');
            return false;
        }
        postData.usernameOrEmailAddress = inp[0].value;
        postData.password = inp[1].value;

        //登陆
        var loginAuthorization = null;
        $.ajax({
            type: 'POST',
            url: API_SERVICE_CBOARD + 'api/Account',
            data: postData,
            async: false,
            xhrFields: {
                withCredentials: true
            },
            success: function(result) {
                // console.log('登录成功', result);
                loginAuthorization = result.result;
                saveCookie('apiKey', loginAuthorization, 1); //登陆成功后保存key，最后一个参数单位为天
                window.sessionStorage.setItem('apiKey', loginAuthorization);
            },
            error: function(error) {
                var title = error ? error.responseJSON.error.message : '网络异常，请联系管理员！';
                var text = error ? error.responseJSON.error.details : null;
                //弹窗提示控件配置
                sweetAlert({
                        title: title, //头部
                        text: text, //主体
                        type: "error", //类型
                        // confirmButtonColor: "#286090",//确认按钮颜色
                        confirmButtonText: "关闭", //确认按钮文本
                    },
                    function(isConfirm) {
                        if (isConfirm) { //确认
                            // location.reload();
                        }
                    }
                );
            },
        });
        if (loginAuthorization) {
            //获取账号信息
            $.ajax({
                type: 'GET',
                headers: {
                    'Access-Control-Allow-Origin': '*',
                    'authorization': 'Bearer ' + loginAuthorization
                },
                url: API_SERVICE_CBOARD + '/api/services/app/session/GetCurrentLoginInformations',
                data: {},
                async: false,
                xhrFields: {
                    withCredentials: true
                },
                success: function(result) {
                    // console.log('账号信息', result);
                    // window.localStorage.setItem('apiKey', loginAuthorization);
                    // window.sessionStorage.setItem('curAccountData-user', JSON.stringify(result.result.user));
                    window.sessionStorage.setItem('apiKey', loginAuthorization);
                    window.sessionStorage.setItem('curAccountData-user', JSON.stringify(result.result.user));
                    window.location.href = '/'; //进入NG应用程序
                },
            });
        }
    };
    //判断是否已登录
    if (window.sessionStorage.getItem('apiKey')) {
        window.location.href = '/';
    }
    //键盘事件
    document.onkeydown = function(event) {
        if (event.keyCode == '13') {
            login();
        }
    };
</script>

</html>